# TODO: Add comment
# 
# Author: rogb
###############################################################################

setClass("CashFlow",
	contains=c("BasicDateData")
)

CashFlow <- function(Currency,Name,Date,Data){
	nCurrency <- length(Currency)
	if(missing(Name)){
		Name=character(nCurrency)
	}
	nName <- length(Name)
	if(nCurrency!=nName & (nName>0 &nCurrency>0)){
		stop("Name and Currency have different length")
	}
	nDate <- length(Date)
	Data <- as.matrix(Data)
	
	Dim <- dim(Data)
	nRowData <- Dim[1]
	nColData <- Dim[2]
	if((nRowData!=nDate) | (nColData!=nCurrency)){
		stop("Data, Name and Currency have different dimension")
	}
	
	new("CashFlow",Currency=Currency,Name=Name,Date=Date,Data=Data)
}
# AAA <- matrix(rnorm(20),nrow=10)
# cf1 <- CashFlow(c("CHF","EUR"),c("Asset1","Asset2"),Sys.Date()+0:9,matrix(rnorm(20),nrow=10))
# CashFlow(Currency=c("CHF","EUR"),Date=Sys.Date()+0:9,Data=1:20)

setMethod("plot",signature=c("CashFlow"),function(x,y,...){
			barplot(t(x@Data),names.arg =x@Date,legend.text=info(x),...)
		})


# x <- CashFlow(Currency = c("CHF","EUR","EUR"),Name=c("A1","A2","L1"),Date=Sys.Date()+0:9,Data=matrix(rnorm(3*10),ncol=3))
# i <- Sys.Date()-1
# j <- 2
# x[Sys.Date()+c(0:2,15),,drop=T]


# object <- CashFlow(Currency = c("CHF","EUR","EUR"),Name=c("A1","A2","L1"),Date=Sys.Date()+0:9,Data=matrix(1:30,ncol=3))
setMethod("aggregateCurrency","CashFlow",function(object){
			Date <- object@Date
			Data <- t(apply(object@Data,1,function(x,idx){
						tapply(x,idx,sum)
					},object@Currency))
			if(length(unique(object@Currency))==1){
				Data <- t(Data)
				Currency <- unique(object@Currency)
				Name <- character(1)
			}
			else{
				Currency <- colnames(Data)
				colnames(Data) <- NULL
				Name <- character(length(Currency))
			}
			CashFlow(Currency=Currency,Name=Name,Date=Date,Data=Data)
		})

# e1 <- CashFlow(Currency = c("CHF","EUR","EUR"),Name=c("A1","A2","L1"),Date=Sys.Date()+0:9,Data=matrix(1:30,ncol=3))
# e2 <- CashFlow(Currency = c("CHF","EUR","USD"),Name=c("A1","A2","L1"),Date=Sys.Date()+0:14,Data=matrix(1:45,ncol=3))

# c1 <- CashFlow(Currency = c("CHF","EUR","EUR"),Name=c("A1","A2","L1"),Date=Sys.Date()+0:9,Data=matrix(1:30,ncol=3))
# c2 <- CashFlow(Currency = c("CHF","EUR","USD"),Name=c("A1","A2","L1"),Date=Sys.Date()+0:14,Data=matrix(1:45,ncol=3))


setMethod("+",signature=c("CashFlow","CashFlow"),function(e1,e2){
			Date <- sort(unique(c(e1@Date,e2@Date)))
			Concatenation1 <- paste(e1@Currency,e1@Name,sep=":split:")
			Concatenation2 <- paste(e2@Currency,e2@Name,sep=":split:")
			Concatenation <- c(Concatenation1,Concatenation2)
			Data1 <- cbind(e1[Date,,drop=FALSE]@Data,e2[Date,,drop=FALSE]@Data)
			Data <- t(apply(Data1,1,function(x,idx){
								tapply(x,idx,sum)
							},Concatenation))
			if(length(unique(Concatenation))==1){
				Data <- t(Data)
				Currency <- unique(e1@Currency)
				Name <- unique(e1@Name)
			}
			else{
				RevConcatenation <- strsplit(colnames(Data),":split:")
				Currency <- as.character(unlist(lapply(RevConcatenation,function(x)x[1])))
				Name <- as.character(unlist(lapply(RevConcatenation,function(x)x[2])))
				colnames(Data) <- NULL
			}
			CashFlow(Currency=Currency,Name=Name,Date=Date,Data=Data)
		})

setMethod("-",signature=c("CashFlow","CashFlow"),function(e1,e2){
			e1 + (-1*e2)		
		})

#e1 <- cf1
#e2 <- cf2
#e1 + e2
# cf1 <- CashFlow("CHF","Rogers CashFlow",Sys.Date()+(0:100)*100,rnorm(101))
# cf2 <- CashFlow(Currency="CHF",Date=Sys.Date()+(10:110)*100,Data=rnorm(101))
# cf3 <- CashFlow("EUR",character(1),Sys.Date()+(20:120)*100,rnorm(101))
# cf4 <- CashFlow("CHF","A1",Sys.Date()+(30:130)*100,rnorm(101))
# now <- proc.time()
# cf5 <- merge(cf1,cf2,cf3,cf4,cf3,cf1,cf3,cf2)
# print(proc.time()-now)
# names(cf5) <- c("A","B","C","")
# plot(cf1+cf2+cf3+cf4)
# plot(merge(cf1,cf2,cf3,cf4),beside=F)
# cf2 <- CashFlow(Sys.Date()+0:20,rnorm(21))
# cf3 <- new("CashFlow",Date=Sys.Date()+0:20,CashFlow=cbind(0:20,-20:0))
# cf4 <- new("CashFlow",Date=Sys.Date()+0:20,CashFlow=cbind(0:20,-20:0))
# x <- cf2